﻿+Ext.define("ProjectDocumentModel", {
    extend: "Ext.data.Model",
    fields: []
});
Ext.define('app.prm.project.ProjectDocument', {
    extend: 'Ext.grid.Panel',
    border: false,
    viewConfig: {
        enableTextSelection: true //可以复制单元格文字
    },
    forceFit: false,
    enableLocking: true,
    columns: [],
    controller: 'project',
    requires: ['app.prm.project.ProjectController'],
    columnLines: true,
    initComponent: function () {
        var me = this;
       
        me.tbar = [
            {
                text: '刷新',
                glyph: 0xf021,
                handler: function () {
                    me.loadData();
                }
            },
            {
                checked: true,
                boxLabel: "文档 | 项目",
                ref: 'phase_top_cheched',
                xtype: 'checkbox',
                listeners: {
                    change: function () {
                        me.project_doc();
                    }
                }
            }
        ];
        me.callParent();
    },
    listeners: {
        afterrender: function () {
            this.loadData();
        },
        celldblclick: 'onDocCelldblclick'
    },
    loadData: function () {
        this.loadmask = new Ext.LoadMask(this, { msg: "loading" });
        this.loadmask.show();
        var main = this;
        main.requestDataNum = 0;
        App.AjaxGet('/api/services/app/document/GetDocumentReportColumn', function (data) {
            //loadPhase
            main.projectDocTypes = data.result;
            main.dataReady();
        });
        App.AjaxGet('/api/services/app/project/GetProjectDocumentReport', function (data) {
            //loadProduct
            main.projectDocs = data.result;
            main.dataReady();
        });
    },
    dataReady: function () {

        this.requestDataNum = this.requestDataNum + 1;
        if (this.requestDataNum > 1) {
            this.loadmask.hide();
            this.project_doc();
        }

    },
    project_doc: function () {
        var main = this;
        var cc = this.query('checkbox')[0].checked;
        if (cc) {
            main.createCategoryColumnFields();
            main.loadProjectCategoryStore();
        } else {
            main.createCategoryProjectColumnFields();
            main.loadCategoryProjectStore();
        }
    },
    createCategoryColumnFields: function () {
        var main = this;
        var columns = [];
        var fields = [];
        //Customer
        columns.push({ header: '客户', dataIndex: 'customer', locked: true });
        columns.push({ header: '项目名称', dataIndex: 'projectName', locked: true });
        columns.push({
            header: '项目编号', dataIndex: 'projectNo', locked: true, renderer:
                        function (val) {
                            return '<span style="color:blue">' + val + '</span>'
                        }
        });
        columns.push({ header: '产品号', dataIndex: 'productNo', align: 'center', locked: true });
        fields.push('customer');
        fields.push('projectName');
        fields.push('projectNo');
        fields.push('productNo');
        fields.push('projectId');

        var projectDocTypes = main.projectDocTypes;


        for (var i = 0; i < projectDocTypes.length; i++) {
            var root = projectDocTypes[i];
            var rootOjb = { header: root.text };
            if (root.leaf) {
                rootOjb.dataIndex = root.text + root.id;
                rootOjb.docType = root.id;
                rootOjb.renderer = function (value, p, record) {
                    if (value != undefined)
                        return '<span style="color:blue">' + value + '</span>'
                    else
                        return '';
                }
                fields.push(rootOjb.dataIndex);
            }
            else {
                rootOjb.columns = [];
                for (var j = 0; j < root.children.length; j++) {
                    var record = root.children[j];
                    var twoOjb = { header: record.text };
                    if (record.leaf) {
                        twoOjb.dataIndex = record.text + record.id;
                        twoOjb.docType = record.id;
                        twoOjb.renderer = function (value, p, record) {
                            if (value != undefined)
                                return '<span style="color:blue">' + value + '</span>'
                            else
                                return '';
                        }
                        fields.push(twoOjb.dataIndex);
                    }
                    else {
                        twoOjb.columns = [];
                        for (var k = 0; k < record.children.length; k++) {
                            var last = record.children[k];
                            var lastObj = {
                                header: last.text, dataIndex: last.text + last.id, docType: last.id, align: 'center', renderer: function (value, p, record) {
                                    if (value != undefined)
                                        return '<span style="color:blue">' + value + '</span>'
                                    else
                                        return '';
                                }
                            };
                            twoOjb.columns.push(lastObj);
                            fields.push(lastObj.dataIndex);
                        }
                    }
                    rootOjb.columns.push(twoOjb);
                }
            }
            columns.push(rootOjb);
        }

        this.summryColumns = columns;
        this.summryFields = fields;
    },
    loadProjectCategoryStore: function () {
        var summryFields = this.summryFields;
        var store = new Ext.data.JsonStore({
            fields: summryFields
        });

        ProjectDocumentModel.removeFields(ProjectDocumentModel.fields);
        ProjectDocumentModel.addFields(summryFields);

        var projectDocs = this.projectDocs;
        for (var i = 0; i < projectDocs.length; i++) {
            var data = projectDocs[i];
            var p = Ext.create("ProjectDocumentModel", {
            });

            for (var j = 0; j < summryFields.length; j++) {
                if (data[summryFields[j]]) {
                    p.set(summryFields[j], data[summryFields[j]]);
                }
            }
            store.add(p);
        }
        store.commitChanges();
        this.reconfigure(store, this.summryColumns);
    },
    createCategoryProjectColumnFields: function () {
        var main = this;
        var columns = [];
        var fields = [];
        fields.push("DoctypeId");

        columns.push({
            header: 'Level1', dataIndex: 'PhaseL1', locked: true, sortable: false, renderer: function (value, metaData, record, rowIndex) {
                return value;
            }
        });
        fields.push('PhaseL1');
        columns.push({
            header: 'Level2', dataIndex: 'PhaseL2', locked: true, sortable: false, renderer: function (value, metaData, record, rowIndex) {
                return value;
            }
        });
        fields.push('PhaseL2');
        columns.push({
            header: 'Level3', dataIndex: 'PhaseL3', locked: true, sortable: false, renderer: function (value, metaData, record, rowIndex) {
                return value;
            }
        });
        fields.push('PhaseL3');
        fields.push('PhaseL4');
        fields.push('PhaseL5');


        var projectDocs = main.projectDocs;
        for (var i = 0; i < projectDocs.length; i++) {
            var record = projectDocs[i];
            var obj = {
                header: record.projectName, projectId: record.projectId, sortable: false, projectNo: record.projectNo, dataIndex: 'pro' + record.projectNo, renderer: function (value, p, record) {
                    //var v = value.split('/')[0];
                    //if (v > 0) {
                    //    return "<b style='color:red;font-size:15px'>" + value + "</b>";
                    //}
                    return value;
                }
            };
            fields.push('pro' + record.projectNo);
            columns.push(obj);

        }
        this.summryColumns = columns;
        this.summryFields = fields;
    },
    loadCategoryProjectStore: function () {
        var main = this;
        var summryFields = main.summryFields;

        var store = new Ext.data.JsonStore({
            fields: summryFields
        });

        ProjectDocumentModel.removeFields(ProjectDocumentModel.fields);
        ProjectDocumentModel.addFields(summryFields);

        var projectDocTypes = main.projectDocTypes;

        var p = Ext.create("ProjectDocumentModel", {
            'PhaseL4': "projectNo",
            'PhaseL5': "",
            'PhaseL1': "",
            'PhaseL2': "",
            'DocTypeId': 0,
            'PhaseL3': "项目编号"
        });
        store.add(p);
        var p = Ext.create("ProjectDocumentModel", {
            'PhaseL4': "productNo",
            'PhaseL5': "",
            'PhaseL1': "",
            'PhaseL2': "",
            'DocTypeId': 0,
            'PhaseL3': "产品号"
        });
        store.add(p);


        for (var i = 0; i < projectDocTypes.length; i++) {
            var root = projectDocTypes[i];
            if (root.leaf) {
                var p = Ext.create("ProjectDocumentModel", {
                    'PhaseL4': root.text,
                    'PhaseL5': root.id,
                    'PhaseL1': "",
                    'PhaseL1': "",
                    'PhaseL2': "",
                    'DocTypeId': root.id,
                    'PhaseL3': root.text
                });
                store.add(p);
            }
            else {
                var level2Index = 0;
                for (var j = 0; j < root.children.length; j++) {
                    var record = root.children[j];
                    level2Index++;
                    if (record.leaf) {
                        var p = Ext.create("ProjectDocumentModel", {
                            'PhaseL4': record.text,
                            'PhaseL5': record.id,
                            'PhaseL1': "",
                            'PhaseL1': "",
                            'DocTypeId': record.id,
                            'PhaseL2': level2Index == 1 ? root.text : "",
                            'PhaseL3': record.text
                        });
                        store.add(p);

                    }
                    else {
                        var level3Index = 0;
                        for (var k = 0; k < record.children.length; k++) {
                            level3Index++;
                            var last = record.children[k];
                            var p = Ext.create("ProjectDocumentModel", {
                                'PhaseL4': last.text,
                                'PhaseL5': last.id,
                                'DocTypeId': last.id,
                                'PhaseL1': level2Index == 1 ? root.text : "",
                                'PhaseL2': level3Index == 1 ? record.text : "",
                                'PhaseL3': last.text
                            });
                            store.add(p);
                        }
                    }
                }
            }
        }
        main.loadDocProjectRecord(store);

        store.commitChanges();
        main.reconfigure(store, this.summryColumns);
    },
    loadDocProjectRecord: function (store) {
        var main = this;
        var projectDocs = main.projectDocs;
        store.each(function (r) {
            var propety1 = r.get('PhaseL4') + r.get('PhaseL5');
            for (var i = 0; i < projectDocs.length; i++) {
                var p = projectDocs[i];
                if (p[propety1]) {
                    r.set('pro' + p['projectNo'], p[propety1]);
                }
            }
        });
    }
});